package com.tom.commonframework.common.base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * SharePreference工具类
 * 
 * @author Ammy
 *
 */
public class SharePreferenceUtils {

	private static final String SHAREPREFERNCESNAME = "runlife.config";
	private static SharedPreferences sp;

	public static SharedPreferences getSharedPreferences(Context context) {
		if (context != null) {
			if (sp == null) {
				sp = context.getSharedPreferences(SHAREPREFERNCESNAME,
						Context.MODE_PRIVATE);
			}
		}
		return sp;
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param value
	 * @return 是否保存成功
	 */
	public static boolean setValue(Context context, String key, Object value) {

		if (context == null) {
			return false;
		}

		Editor edit = getSharedPreferences(context).edit();

		if (value instanceof String) {
			return edit.putString(key, (String) value).commit();
		} else if (value instanceof Boolean) {
			return edit.putBoolean(key, (Boolean) value).commit();
		} else if (value instanceof Float) {
			return edit.putFloat(key, (Float) value).commit();
		} else if (value instanceof Integer) {
			return edit.putInt(key, (Integer) value).commit();
		} else if (value instanceof Long) {
			return edit.putLong(key, (Long) value).commit();
		} else if (value instanceof Set) {
			new IllegalArgumentException("Value can not be Set object!");
			return false;
		}
		return false;
	}

	public static boolean setValue(Context context, String key, Set<String> set) {
		if (context == null) {
			return false;
		}

		Editor edit = getSharedPreferences(context).edit();
		return edit.putStringSet(key, set).commit();
	}

	public static void setValues(Context context, String[] keys, Object[] values) {

		if (context == null || keys == null || values == null) {
			return;
		}

		if (keys.length == 0 || values.length == 0) {
			return;
		}

		if (keys.length == values.length) {
			int len = keys.length;

			for (int i = 0; i < len; i++) {
				setValue(context, keys[i], values[i]);
			}
		}
	}

	public static Set<String> getStringSet(Context context, String key) {
		return getSharedPreferences(context).getStringSet(key, new TreeSet<String>());
	}

	public static List<Object> getValues(Context context, String[] keys) {

		List<Object> values = new ArrayList<Object>();

		if (keys != null && keys.length > 0) {
			SharedPreferences sp = getSharedPreferences(context);
			Map<String, ?> mapValues = sp.getAll();
			int len = keys.length;
			for (int i = 0; i < len; i++) {
				values.add(mapValues.get(keys[i]));
			}
		}

		return values;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getValue(Context context, Class<T> clazz, String key) {

		T t = null;
		if (!TextUtils.isEmpty(key)) {
			SharedPreferences sp = getSharedPreferences(context);
			Map<String, ?> mapValues = sp.getAll();

			t = (T) mapValues.get(key);

		}
		return t;
	}

	public static boolean getBoolean(Context context, String key) {
		return getSharedPreferences(context).getBoolean(key, false);
	}

	public static String getString(Context context, String key) {

		return getSharedPreferences(context).getString(key, "");
	}

	public static Float getFloat(Context context, String key) {
		return getSharedPreferences(context).getFloat(key, 0f);
	}

	public static int getInt(Context context, String key) {
		return getSharedPreferences(context).getInt(key, 0);
	}

	public static long getLong(Context context, String key) {
		return getSharedPreferences(context).getLong(key, 0);
	}
	
	public static void remove(Context context, String... key){
		for(int i = 0; i< key.length; i++){
			getSharedPreferences(context).edit().remove(key[i]).commit();
		}
	}
	public static void remove(Context context, String key) {
		getSharedPreferences(context).edit().remove(key).commit();
	}

    public static void removeAll(Context context) {
	  getSharedPreferences(context).edit().clear().commit();
    }

}
